<?php
namespace Application\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
class CountryTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }	
	public function getallcountries()
    {	
        $select = $this->tableGateway->getSql()->select();		
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	        
    }
	public function getcountry($data)
    {
		$select = $this->tableGateway->getSql()->select();
		$select->where('country_id="'.$data.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->current();
        return $row;
	}
	public function checkoncountry($country_name){
		$select = $this->tableGateway->getSql()->select();
		$select->where('country_name="'.$country_name.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->current();
        return $row;
	}
	public function addCountry($country){
	    $country_name=Strtoupper($country['country_name']);
		$data = array(				
			'country_name' => $country_name,  		
			'iso'  		   => $country_name,
			'nicename'     => ucfirst($country['country_name']),
			'iso3' 		   => $country_name, 				
			'numcode' 	   =>	0, 				
			'phonecode'    =>	0,
            'currency'	   =>$country['currency'],	
		);
		if(isset($country['country_id']) && $country['country_id']==""){
			$this->tableGateway->insert($data);	
		}else{
			$this->tableGateway->update($data, array('country_id' => $country['country_id']));
		}		
		return $this->tableGateway->lastInsertValue;
	
	}
	public function deleteCountry($country_id){
		$deleteid=$this->tableGateway->delete(array('(country_id IN ('.$country_id.'))'));
		return $deleteid;
	}
	public function getCountryPrice($country_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('country_id="'.$country_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		$row = $resultSet->current();
        return $row;
	}	
}